package com.imooc.feeds.controller;

import com.imooc.commons.model.domain.ResultInfo;
import com.imooc.commons.model.pojo.Feeds;
import com.imooc.commons.model.vo.FeedsVO;
import com.imooc.commons.utils.ResultInfoUtil;
import com.imooc.feeds.service.FeedsService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Path;
import java.util.List;

/**
 * @author gcq
 * @Create 2022-11-16
 */
@RestController
public class FeedsController {

    @Resource
    private FeedsService feedsService;
    @Resource
    private HttpServletRequest servletRequest;


    /**
     * 分页获取我关注的 feeds
     *
     * @param page
     * @param access_token
     * @return
     */
    @GetMapping("{page}")
    public ResultInfo selectForPage(@PathVariable Integer page, String access_token){
        List<FeedsVO> feedsVOS = feedsService.selectForPage(page, access_token);
        return ResultInfoUtil.buildSuccess(servletRequest.getServletPath(), feedsVOS);
    }

    /**
     * 变更 Feed
     * 当A用户关注B用户时，那么要实时的将B的所有Feed推送到A用户的Feed集合中，
     * 同样如果A取关B用户，那么需要将B用户所有的Feed从A用户中的Feed集合中移除。
     *
     * @param followingDinerId 关注的好友的 ID
     * @param access_token      访问token
     * @param type              1 关注 0 取关
     * @return
     */
    @PostMapping("/updateFollowingFeeds/{followingDinerId}")
    public ResultInfo addFollowingFeeds(@PathVariable Integer followingDinerId,
                                        String access_token,
                                        @RequestParam Integer type) {
        feedsService.addFollowingFeed(followingDinerId, access_token, type);
        return ResultInfoUtil.buildSuccess(servletRequest.getServletPath(), "操作成功");
    }

    /**
     * 删除 feeds
     * 删除feeds内容
     * 同时将feed内容从当前用户的粉丝列表中移除
     *
     * @param id feeds id
     * @param access_token 访问token
     */
    @DeleteMapping("/{id}")
    public ResultInfo delete(@PathVariable Integer id, String access_token){
        feedsService.delete(id, access_token);
        return ResultInfoUtil.buildSuccess(servletRequest.getServletPath(), "删除成功");
    }

    /**
     * 添加 Feed
     * 将feed内容添加至数据库、同时将feed内容推送给当前用户的粉丝列表
     *
     * @param feeds         feeds
     * @param access_token 访问token
     */
    @PostMapping
    public ResultInfo create(@RequestBody Feeds feeds, String access_token) {
        feedsService.create(feeds, access_token);
        return ResultInfoUtil.buildSuccess(servletRequest.getServletPath(), "添加成功");
    }
}